package com.sheepone.util.time.getter;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author Miss.杨
 * @description
 * @since 2025/3/13 - 21:20
 */
public class LocalDateTimeGetter {
    private LocalDateTimeGetter() {
    }

    public static LocalDateTime today() {
        return LocalDateTime.of(LocalDate.now(ZoneId.systemDefault()), LocalTime.MIN);
    }

    public static LocalDateTime fromDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


    public static LocalDateTime fromString(String dateStr, String pattern) {
        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDateTime fromStamp(long stamp, TimeUnit timeUnit) {
        long nanos = timeUnit.toNanos(stamp);
        long seconds = timeUnit.toSeconds(stamp);

        Instant instant = Instant.ofEpochSecond(seconds, nanos % 1_000_000_000);

        // 将Instant对象转换为LocalDateTime，这里假设是在系统默认时区
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }
}
